package com.qiyuexinxi.chat.service.impl;

import com.qiyuexinxi.chat.exception.reg.exception.BizErrorCodeEnum;
import com.qiyuexinxi.chat.exception.reg.exception.BizException;
import com.qiyuexinxi.chat.feign.SendMessageFeign;
import com.qiyuexinxi.chat.service.SendMessageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangxingyu
 * @date 2020/2/28
 * @description
 */
@Service
public class SendMessageServiceImpl implements SendMessageService {

    @Value("${message.uid}")
    private Integer uid;
    @Value("${message.password}")
    private String password;
    @Value("${message.msg}")
    private String msg;
    @Value("${message.url}")
    private String url;

    @Autowired
    SendMessageFeign sendMessage;

    @Override
    public void sendMessage(String phone,String code){
        String message= StringUtils.replace(msg, "%s", code);
        Map<String,Object> sendMessageRequest = new HashMap<String,Object>();
        sendMessageRequest.put("uid",uid);
        sendMessageRequest.put("password",password);
        sendMessageRequest.put("mobile",phone);
        sendMessageRequest.put("msg",message);
        Map<String, Object> map = sendMessage.sendPhoneCodeGet(uid,password,phone,message);
        if(map==null){
            throw new BizException(BizErrorCodeEnum.SENDING_MESSAGE_FAILED);
        }
        Object code1 = map.get("code");
        if(code1 instanceof Integer){
            if((int)code1!=0){
                throw new BizException(BizErrorCodeEnum.SENDING_MESSAGE_FAILED);
            }
        }

    }
}
